import axios from 'axios'
const base = '/api/isv'
import ElementUI from 'element-ui'
import $store from "@/store/index"



// 报名人数
async function getSingnList() {
  return new Promise((resolve) => {
    let resData = {
      success: false,
      errorMsg: '',
      data: []
    }
    axios.post(`${base}/infopsn/signnumber`, {
      activityId: $store.state.thisActivityId,
      stratDate: $store.state.singnTime.stratDate,
      endDate: $store.state.singnTime.endDate
    }).then(res => {
      if (res.data.success) {
        $store.state.showSingnChart = false;
        resData.success = true;
        resData.data = res.data.result;
        $store.state.singnList = resData.data;
        let xName = [];
        let dataNum = [];
        let oneData = [];
        if ($store.state.singnList.list.length != 0) {
          $store.state.singnList.list.forEach(item => {
            xName.push(item.triggerDay);
            dataNum.push(item.triggerCount);
          });
        }
        oneData.push({
          name: "报名人数",
          num: dataNum,
          content: xName
        });
        let dataList = [];
        dataList.push(...oneData);
        $store.state.lineChartDataSingn.barData = dataList;
        $store.state.lineChartDataSingn.total = $store.state.singnList.num;
        $store.state.showSingnChart = true;
      } else {
        resData.errorMsg = res.data.errorMsg || '网络异常';
        ElementUI.Message.warning({
          message: resData.errorMsg,
          type: 'warning'
        })
      }
      resolve(resData);
    }, err => {
      ElementUI.Message.error(`网络异常`);
      resolve(resData);
    });
  });
}
export {
  getSingnList
}



// 请求数据的公共方法
async function pubServiec(url) {
  return new Promise((resolve) => {
    let resData = {
      success: false,
      errorMsg: '',
      data: []
    }
    axios.post(`${base}${url}`, {
      activityId: $store.state.thisActivityId,
    }).then(res => {
      if (res.data.success) {
        resData.success = true;
        resData.data = res.data.result;
      } else {
        resData.errorMsg = res.data.errorMsg || '网络异常';
        ElementUI.Message.warning({
          message: resData.errorMsg,
          type: 'warning'
        })
      }
      resolve(resData);
    }, err => {
      ElementUI.Message.error(`网络异常`);
      resolve(resData);
    });
  });
}





// 得到表格数据
async function getPopTablist(routeName) {
  if (!routeName) {
    ElementUI.Message.warning({
      message: '网络异常',
      type: 'warning'
    })
    return;
  }
  let title = ''; //标题名称
  let url = ''; //请求的url
  let xName = ''; //
  if (routeName == "Sex") {
    // 人员统计-性别统计
    title = '性别';
    url = '/infopsn/gender';
    xName = 'gender';
  } else if (routeName == "Age") {
    // 人员统计-年龄统计
    title = '年龄';
    url = '/infopsn/agedesc';
    xName = 'age';
  } else if (routeName == "Province") {
    // 人员统计-省份分析
    title = '省份';
    url = '/infopsn/province';
    xName = 'province';
  } else if (routeName == "Industry") {
    // 人员统计-行业分析
    title = '行业';
    url = '/infopsn/industry';
    xName = 'industry';
  } else if (routeName == "Occupation") {
    // 人员统计-职业统计
    title = '职业';
    url = '/infopsn/job';
    xName = 'job';
  } else if (routeName == "Nationality") {
    // 人员统计-国籍分析
    title = '国籍';
    url = '/infopsn/country';
    xName = 'country';
  } else if (routeName == "Scale") {
    // 人员统计-公司规模
    title = '规模';
    url = '/infopsn/scale';
    xName = 'scale';
  } else if (routeName == "Abroad") {
    // 人员统计-国内外分析
    title = '国内外';
    url = '/infopsn/abroad';
    xName = 'home';
  }



  $store.state.pageLoading = true;
  let resData = await pubServiec(url);
  $store.state.pageLoading = false;

  if (resData.success) {
    console.log(resData.data);
    $store.state.showPopTablistChart = false;
    $store.state.popTablistChart.xName = [];
    $store.state.popTablistChart.barData = [];
    $store.state.popTablistChart.showName = [title];
    if (routeName == "Sex") {
      let dataList = [{
          name: '男',
          num: resData.data.list.manMap.manSum
        },
        {
          name: '女',
          num: resData.data.list.womanMap.womanSum
        }
      ]
      $store.state.popTablist = dataList
      if (dataList.length != 0) {
        dataList.forEach(item => {
          $store.state.popTablistChart.xName.push(item.name)
          $store.state.popTablistChart.barData.push(item.num)
        })
      }
      $store.state.popTablistChart.total = resData.data.num;
    } else if (routeName == "Age") {
      let dataList = [];
      if (resData.data.list.twentyMap) {
        dataList.push({
          name: '0-20',
          num: resData.data.list.twentyMap.twentySum
        });
      }
      if (resData.data.list.thirtyMap) {
        dataList.push({
          name: '20-30',
          num: resData.data.list.thirtyMap.thirtySum
        });
      }
      if (resData.data.list.fortyMap) {
        dataList.push({
          name: '30-40',
          num: resData.data.list.fortyMap.fortySum
        });
      }
      if (resData.data.list.fiftyMoreMap) {
        dataList.push({
          name: '40-50',
          num: resData.data.list.fiftyMoreMap.fiftyMoreSum
        });
      }
      if (resData.data.list.fiftyMap) {
        dataList.push({
          name: '50-60',
          num: resData.data.list.fiftyMap.fiftySum
        });
      }
      $store.state.popTablist = dataList
      if (dataList.length != 0) {
        dataList.forEach(item => {
          $store.state.popTablistChart.xName.push(item.name)
          $store.state.popTablistChart.barData.push(item.num)
        })
      }
      $store.state.popTablistChart.total = resData.data.num;
    } else {
      $store.state.popTablist = resData.data;
      if ($store.state.popTablist.list.length != 0) {
        $store.state.popTablist.list.forEach(item => {
          if (item[xName]) {
            $store.state.popTablistChart.xName.push(item[xName]);
            $store.state.popTablistChart.barData.push(item.num);
          }
        });
      }
    }
    $store.state.popTablistChart.total = $store.state.popTablist.num;
    $store.state.showPopTablistChart = true;
  }

  return new Promise((resolve) => {
    resolve(resData);
  });
}
export {
  getPopTablist
}
